package com.hotdog.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 本类主要获取支付连接
 *
 * @author dell
 */
@Service
public class PayConfig {
    @Value("${pay.serverUrl}")
    private String serverUrl;
    @Value("${pay.appId}")
    private String appId;
    @Value("${pay.privateKey}")
    private String privateKey;
    @Value("${pay.format}")
    private String format;
    @Value("${pay.charset}")
    private String charset;
    @Value("${pay.alipayPublicKey}")
    private String alipayPublicKey;
    @Value("${pay.signType}")
    private String signType;
    @Value("${pay.returnUrl}")
    private String returnUrl;

    public String geturl(String order) throws AlipayApiException {
        System.out.println(serverUrl);
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();

        request.setBizContent("{" +
                "\"out_trade_no\":\"" + order + "\"," +
                "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
                "\"total_amount\":9999," +
                "\"subject\":\" 会员购\"}"
        );
        /*return_url和notify_url这两个参数，前者是支付成功跳转的页面，后者是支付状态发生改变（如：用户付款完成）请求的url路径。*/
        request.setReturnUrl("http://localhost:9856/nono/pay/return");
        request.setNotifyUrl("http://localhost:9856/nono/alipay/hellopay");

        AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "GET");

        return response.getBody();

    }
}
